#! /bin/sh

RESOLVER_DIR='/etc/resolver'

. ./common.inc

[ -r "$EXCEPTIONS_FILE" ] &&
  DOMAINS_EXCEPTIONS="$(cat "$EXCEPTIONS_FILE") $DOMAINS_EXCEPTIONS"

get_gw() {
  route -n get default | while read line; do
    case "$line" in
      gateway:\ *)
        echo "$line" | sed 's/ *gateway: *//'
        return
      ;;
    esac
  done
}

get_dhcp_dns() {
  cat "${STATES_DIR}/dhcp-dns" 2> /dev/null
}

remove_old_exceptions() {
  local found

  find -x "$RESOLVER_DIR" -type f -print | {
    while read file; do
      found='no'
      for domain in $DOMAINS_EXCEPTIONS; do
        [ "$file" = "${RESOLVER_DIR}/${domain}" ] && found='yes' && break
      done
      [ "$found" = 'no' ] || continue
      fgrep -ci 'automatically generated by the dnscrypt user interface' \
        $file > /dev/null 2>&1 || continue
      rm -f "$file"
    done
  }
}

name_servers=$(./get-static-resolvers.sh || get_dhcp_dns || get_gw)
[ x"$name_servers" = 'x' ] && exit 0

default_domain=$(./get-current-default-domain.sh)

mkdir -p "$RESOLVER_DIR" || exit 1

remove_old_exceptions

name_server="$gw"
for domain in $DOMAINS_EXCEPTIONS; do
  if [ x"$default_domain" != 'x' ]; then
    echo "$domain" | grep -c ".*${default_domain}" > /dev/null
    [ $? = 0 ] && continue
  fi
  echo '# automatically generated by the dnscrypt user interface' \
    > "${RESOLVER_DIR}/${domain}"
  for name_server in $name_servers; do
    echo "nameserver ${name_server}" >> "${RESOLVER_DIR}/${domain}"
  done
done
exit 0
